package com.walk.zworld.ui.fragment

import android.view.View
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.walk.zworld.R
import com.walk.zworld.mvvm.viewModel.SystemViewModel
import com.base.lib.base.view.BaseMvvmFragment


class SystemFragment : BaseMvvmFragment<SystemViewModel>() {

    companion object {
        fun newsInstance() = SystemFragment()
    }

    override fun onBindViewModel(): Class<SystemViewModel> = SystemViewModel::class.java

    override fun onBindLayout(): Int  = R.layout.fragment_work

    private lateinit var vp2: ViewPager2
    private lateinit var tbLayout: TabLayout
    private val titleList = mutableListOf<String>()
    private val fragmentList = mutableListOf<Fragment>()
    private val systemPagerAdapter by lazy { SystemPagerAdapter() }

    override fun initData() {}

    override fun initView(view: View) {
        vp2 = findViewById(R.id.vp2_system)
        tbLayout = findViewById(R.id.tl_system)
        titleList.add(resources.getString(R.string.system))
        titleList.add(resources.getString(R.string.navigation))
        fragmentList.add(SysKnowledgeFragment.newInstance())
        fragmentList.add(SysNavigationFragment.newInstance())
        vp2.adapter = systemPagerAdapter
        tbLayout.run {
            addOnTabSelectedListener(onTabSelectedListener)
        }
        TabLayoutMediator(tbLayout, vp2) { tab, position ->
            tab.text = titleList[position]
        }.attach()
    }

    private val onTabSelectedListener = object : TabLayout.OnTabSelectedListener {
        override fun onTabReselected(tab: TabLayout.Tab?) {
        }

        override fun onTabUnselected(tab: TabLayout.Tab?) {
        }

        override fun onTabSelected(tab: TabLayout.Tab?) {
            tab?.let {
                vp2.currentItem = it.position
            }
        }
    }

    inner class SystemPagerAdapter : FragmentStateAdapter(this) {

        override fun getItemCount(): Int = fragmentList.size

        override fun createFragment(position: Int): Fragment {
            return when (position) {
                0 -> SysKnowledgeFragment.newInstance()
                1 -> SysNavigationFragment.newInstance()
                else -> SysKnowledgeFragment.newInstance()
            }
        }
    }
}